package com.yun.datastructure.tree.binarytree.leetcode;

import com.yun.datastructure.tree.binarytree.domain.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Leetcode111 {
    public int minDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        int dl=minDepth(root.left);
        int dr=minDepth(root.right);
        if(dr==0){
            return dl+1;
        }
        if(dl==0){
            return dr+1;
        }
        return Math.min(dl,dr)+1;
    }

    public int minDepth1(TreeNode root){
        if(root==null){
            return 0;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        int depth=0;
        while (!queue.isEmpty()){
            int size= queue.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode first=queue.poll();
                if(first.left==null&&first.right==null){
                    return depth;
                }
                if(first.left!=null){
                    queue.offer(first.left);
                }
                if(first.right!=null){
                    queue.offer(first.right);
                }
            }
        }
        return depth;
    }
}
